//    AVPlayer *player = [[AVPlayer playerWithPlayerItem:playerItem]retain];
//    AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:player];
//    playerLayer.frame = CGRectMake(0, 0, 1024, 768);
//    playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;

CALayer *gerald = [CALayer layer];
gerald.frame = CGRectMake(0, 0, 120, 120);
gerald.contents = (id) [UIImage imageNamed:@"rightNudgeButton.png"].CGImage;

//    [self.view.layer addSublayer:playerLayer];
//    [self.view.layer insertSublayer:gerald above:playerLayer];

#if 0
CAKeyframeAnimation *anim = [CAKeyframeAnimation animationWithKeyPath:@"position"];
anim.duration = 18.0;

anim.values = [NSArray arrayWithObjects:
//                   [NSValue valueWithCGRect:CGRectMake(20.0, 100.0, 50.0, 50.0)],
//                   [NSValue valueWithCGRect:CGRectMake(110.0, 100.0, 50.0, 50.0)],
//                   [NSValue valueWithCGRect:CGRectMake(220.0, 100.0, 50.0, 50.0)],
//                   [NSValue valueWithCGRect:CGRectMake(310.0, 100.0, 50.0, 50.0)],
//                   [NSValue valueWithCGRect:CGRectMake(220.0, 100.0, 50.0, 50.0)],
//                   [NSValue valueWithCGRect:CGRectMake(110.0, 100.0, 50.0, 50.0)],
//                   [NSValue valueWithCGRect:CGRectMake(80.0, 100.0, 50.0, 50.0)],
//                   [NSValue valueWithCGRect:CGRectMake(20.0, 100.0, 50.0, 50.0)],
//                   [NSValue valueWithCGRect:CGRectMake(110.0, 100.0, 50.0, 50.0)],
//                   [NSValue valueWithCGRect:CGRectMake(220.0, 100.0, 50.0, 50.0)],
//                   [NSValue valueWithCGRect:CGRectMake(310.0, 100.0, 50.0, 50.0)],
//                   [NSValue valueWithCGRect:CGRectMake(220.0, 100.0, 50.0, 50.0)],
//                   [NSValue valueWithCGRect:CGRectMake(110.0, 100.0, 50.0, 50.0)],
//                   [NSValue valueWithCGRect:CGRectMake(220.0, 100.0, 50.0, 50.0)],
//                   [NSValue valueWithCGRect:CGRectMake(310.0, 100.0, 50.0, 50.0)],
//                   [NSValue valueWithCGRect:CGRectMake(220.0, 100.0, 50.0, 50.0)],
//                   [NSValue valueWithCGRect:CGRectMake(110.0, 100.0, 50.0, 50.0)], nil];
[NSValue valueWithCGPoint:CGPoint(x: 20.0, 100.0)],
[NSValue valueWithCGPoint:CGPoint(x: 110.0, 100.0)],
[NSValue valueWithCGPoint:CGPoint(x: 220.0, 100.0)],
[NSValue valueWithCGPoint:CGPoint(x: 310.0, 100.0)],
[NSValue valueWithCGPoint:CGPoint(x: 220.0, 100.0)],
[NSValue valueWithCGPoint:CGPoint(x: 110.0, 100.0)],
[NSValue valueWithCGPoint:CGPoint(x: 80.0, 100.0)],
[NSValue valueWithCGPoint:CGPoint(x: 20.0, 100.0)],
[NSValue valueWithCGPoint:CGPoint(x: 110.0, 100.0)],
[NSValue valueWithCGPoint:CGPoint(x: 220.0, 100.0)],
[NSValue valueWithCGPoint:CGPoint(x: 310.0, 100.0)],
[NSValue valueWithCGPoint:CGPoint(x: 220.0, 100.0)],
[NSValue valueWithCGPoint:CGPoint(x: 110.0, 100.0)],
[NSValue valueWithCGPoint:CGPoint(x: 220.0, 100.0)],
[NSValue valueWithCGPoint:CGPoint(x: 310.0, 100.0)],
[NSValue valueWithCGPoint:CGPoint(x: 220.0, 100.0)],
[NSValue valueWithCGPoint:CGPoint(x: 110.0, 100.0)], nil];


anim.keyTimes = [NSArray arrayWithObjects:
[NSNumber numberWithFloat:0.02],
[NSNumber numberWithFloat:0.06],
[NSNumber numberWithFloat:0.09],
[NSNumber numberWithFloat:0.1],
[NSNumber numberWithFloat:0.12],
[NSNumber numberWithFloat:0.2],
[NSNumber numberWithFloat:0.25],
[NSNumber numberWithFloat:0.32],
[NSNumber numberWithFloat:0.38],
[NSNumber numberWithFloat:0.49],
[NSNumber numberWithFloat:0.53],
[NSNumber numberWithFloat:0.59],
[NSNumber numberWithFloat:0.63],
[NSNumber numberWithFloat:0.69],
[NSNumber numberWithFloat:0.78],
[NSNumber numberWithFloat:0.81],
[NSNumber numberWithFloat:0.91],
[NSNumber numberWithFloat:1.0], nil];
#endif

#if 1
CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"bounds"];
CGRect oldBounds = self.collectionView.bounds;
anim.fromValue = [NSValue valueWithCGRect:CGRectMake(oldBounds.origin.x - 300, oldBounds.origin.y, oldBounds.size.width, oldBounds.size.height)];
CGRect toRect = CGRectMake(oldBounds.origin.x + 300, oldBounds.origin.y, oldBounds.size.width, oldBounds.size.height);
anim.toValue = [NSValue valueWithCGRect:toRect];
anim.removedOnCompletion = NO;
anim.beginTime = AVCoreAnimationBeginTimeAtZero;
anim.duration = 1.0; //CMTimeGetSeconds(playerItem.duration);
anim.fillMode = kCAFillModeForwards;
anim.delegate = self;
[self.collectionView.layer addAnimation:anim forKey:@"transform.position"];

// animate the position so it stays at 0, 0 of the frame.
anim = [CABasicAnimation animationWithKeyPath:@"position"];
anim.fromValue = [NSValue valueWithCGPoint:CGPoint(x: oldBounds.size.width/2, oldBounds.size.height/2)];
anim.toValue = [NSValue valueWithCGPoint:CGPoint(x: oldBounds.size.width/2, oldBounds.size.height/2)];
anim.removedOnCompletion = NO;
anim.beginTime = AVCoreAnimationBeginTimeAtZero;
anim.duration = CMTimeGetSeconds(playerItem.duration);
anim.fillMode = kCAFillModeForwards;
anim.delegate = self;
[gerald addAnimation:anim forKey:@"PositionAnimation"];
#endif


AVSynchronizedLayer *syncLayer = [AVSynchronizedLayer synchronizedLayerWithPlayerItem:playerItem];
[syncLayer addSublayer:gerald];
[self.view.layer insertSublayer:syncLayer below:self.collectionView.layer];

#if 0

// Configure basic animation to animate the x position of the playhead
CGRect origBounds = self.collectionView.bounds;
CABasicAnimation *scrollAnimation = [CABasicAnimation animationWithKeyPath:@"bounds"];
scrollAnimation.fromValue = [NSValue valueWithCGRect:self.collectionView.bounds];
//    CGRect toRect = CGRectMake([self xPositionForTime:playerItem.duration].floatValue, origBounds.origin.y, origBounds.size.width, origBounds.size.height);
CGRect toRect = CGRectMake(1200, origBounds.origin.y, origBounds.size.width, origBounds.size.height);
scrollAnimation.toValue = [NSValue valueWithCGRect:toRect];
scrollAnimation.removedOnCompletion = NO;
scrollAnimation.beginTime = AVCoreAnimationBeginTimeAtZero;
scrollAnimation.duration = CMTimeGetSeconds(playerItem.duration);
scrollAnimation.fillMode = kCAFillModeBoth;
[self.collectionView.layer addAnimation:scrollAnimation forKey:nil];

// Create AVSynchronizedLayer to synchronize with player's timing
AVSynchronizedLayer *syncLayer = [AVSynchronizedLayer synchronizedLayerWithPlayerItem:playerItem];
[self.view.layer insertSublayer:syncLayer below:self.collectionView.layer];

// Trigger redraw to update UI
[self.view.layer setNeedsDisplay];
#endif
